package dr.mini.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import dr.mini.dao.MemberDao;
import dr.mini.dao.MemberDaoImpl;
import dr.mini.domain.MemberCommand;
 
@Controller
public class SelectListController{

	private static Log log = LogFactory.getLog(SelectListController.class);
	
	private int pageSize = 10;
	
	private MemberDao memberDao;
	@Autowired
	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}

	@RequestMapping("/list.do")
	public ModelAndView process(@RequestParam(value="pageNum", required=false) String pageNum) {
		   
		if(pageNum == null){
			pageNum = "1";
		}
		int currentPage = Integer.parseInt(pageNum);
		int startRow = (currentPage - 1) * pageSize + 1;
		int endRow = currentPage * pageSize;
		
		HashMap<String,Object> map = new HashMap<String,Object>();		
		 
		map.put("start", startRow);
		map.put("end", endRow);
		
		if (log.isDebugEnabled()) {
			log.debug("memberDaoImpl : " + memberDao);
		}
		
		int count = memberDao.getMemberCount();
		List<MemberCommand> list = null;
		if(count >0){
			list = memberDao.getMemberList(map);
		}else{
			list = Collections.emptyList();
		}
		   
		ModelAndView mav = new ModelAndView();
		mav.setViewName("selectList");
		mav.addObject("count", count);
		mav.addObject("currentPage", currentPage);
		mav.addObject("pageSize", pageSize);
		mav.addObject("list",list);
		  
		return mav;
	}
}
